package com.lishu.carsafe.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lishu.carsafe.R;
import com.lishu.carsafe.model.TaskModel;
import com.lishu.carsafe.utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;

public class TaskListAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<TaskModel.TaskBean> taskList = new ArrayList<>();

    public TaskListAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }

    public void setData(List<TaskModel.TaskBean> taskList){
        this.taskList = taskList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return taskList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return taskList.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(view == null) {
            viewHolder = new ViewHolder();
            view = mInflater.inflate(R.layout.adapter_common_list, null);
            viewHolder.icon = view.findViewById(R.id.icon);
            viewHolder.content = view.findViewById(R.id.content);
            viewHolder.title = view.findViewById(R.id.title);
            viewHolder.time = view.findViewById(R.id.time);

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        TaskModel.TaskBean taskBean = taskList.get(position);
        String state = taskBean.getState();
        if("0".equals(state)){
            viewHolder.icon.setImageResource(R.drawable.icon_current_task);
        }else{
            viewHolder.icon.setImageResource(R.drawable.icon_history_task);
        }
        viewHolder.title.setText(taskBean.getTitle());
        viewHolder.content.setText(taskBean.getContent());

        //String taskTime = TimeUtil.getMessageTime(taskBean.getStartTime());
        String taskTime = taskBean.getStartTime();
        if(taskTime != null) {
            viewHolder.time.setText(taskTime);
        }

        return view;
    }

    class ViewHolder {
        ImageView icon;
        TextView content;
        TextView title;
        TextView time;
    }
}
